package com.dw.monitor.tool;

import java.sql.*;

public class testJdbc {
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://172.16.2.163:3306/dashboard_monitor?useUnicode=true&zeroDateTimeBehavior=convertToNull&autoReconnect=true&characterEncoding=utf-8&allowMultiQueries=true";
    static final String USER = "dw_user";
    static final String PASSWORD = "wfg457tgdjk";

    public static Connection getConn(){

        Connection conn = null;
        //注册jdbc驱动
        try {
            Class.forName(JDBC_DRIVER);
            //打开链接
            System.out.println("连接数据库......");
            conn = DriverManager.getConnection(DB_URL,USER,PASSWORD);
            System.out.println("完成数据连接");
        } catch (SQLException e) {
            e.printStackTrace();//处理JDBC错误
        }catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return conn;
    }

    public static void getSelect(String sql){
        Statement stmt  = null;
        try {
            System.out.println("实例化statement对象...");
            stmt = testJdbc.getConn().createStatement();
            ResultSet rs = stmt.executeQuery(sql);
            //展开结果集数据库
            while(rs.next()){
                int id = rs.getInt("id");
                String cluster_name = rs.getString("cluster_name");
                String entity_name = rs.getString("entity_name");
                String type = rs.getString("type");
                String date_f = rs.getString("date_f");
                String time_stamp = rs.getString("time_stamp");
                int value = rs.getInt("value");

                System.out.print("id:"+id);
                System.out.print(",cluster_name:"+cluster_name);
                System.out.print(",entity_name:"+entity_name);
                System.out.print(",type:"+type);
                System.out.print(",date_f:"+date_f);
                System.out.print(",time_stamp:"+time_stamp);
                System.out.print(",value:"+value);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    //插入数据
    public static void getUpdate(String sql){


    }

    public static void main(String[] args) {
        String sql = "select id,cluster_name,entity_name,type,date_f,time_stamp,value from dashboard_monitor_cdh_data";
        testJdbc.getSelect(sql);
        System.out.println("Goodbye!");
    }
}
